<?php

namespace App\Http\Middleware;

use App\Http\Controllers\Controller;
use Closure;
use App\Http\Controllers\System\EncryptController;

class Security
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $inputString         = $request->input('param');
        if (config('app.debug') == true && empty($inputString)) {
            session(['debug'=>true],false);
            return $next($request);
        } else {
            session(['debug'=>false],false);
            if (empty($inputString)){
                return Controller::response(null,-98,'网络错误!');
            }
            $key                        = md5(date('Ymd'));
            $encyptCon                  = new EncryptController();
            $input                      = $encyptCon->authCode($inputString,'DECODE',$key);
            if (is_string($input)){
                $input                  = json_decode($input,true);
            }
            if (is_array($input)){
                $request->replace($input);
                return $next($request);
            }
            return Controller::response(['time'=>time(),'timezone'=>date_default_timezone_get()],-98,'网络错误!');
        }
    }
}
